package com.xiaoshuidi.cloud.module.finance.controller.app.invoice;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.LoginUser;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.finance.controller.app.invoice.vo.AppFinanceInvoiceApplyReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.app.invoice.vo.AppFinanceInvoicePageReqVO;
import com.xiaoshuidi.cloud.module.finance.controller.app.invoice.vo.AppFinanceInvoiceVO;
import com.xiaoshuidi.cloud.module.finance.convert.finance.InvoiceConvert;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.invoice.FinanceInvoiceRecordDO;
import com.xiaoshuidi.cloud.module.finance.enums.ErrorCodeConstants;
import com.xiaoshuidi.cloud.module.finance.enums.finance.FinanceCustomerTypeEnum;
import com.xiaoshuidi.cloud.module.finance.service.invoice.FinanceInvoiceRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Tag(name = "租客 APP - 发票")
@Slf4j
@RestController
@RequestMapping("/finance/invoice")
public class AppFinanceInvoiceController {

    @Resource
    private FinanceInvoiceRecordService financeInvoiceRecordService;

    /**
     * 租客申请开票
     * @return
     */
    @PostMapping("/apply")
    @Operation(summary = "申请开票")
    public CommonResult<Void> apply(@Valid @RequestBody AppFinanceInvoiceApplyReqVO reqVO) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        financeInvoiceRecordService.apply(user, reqVO);
        return CommonResult.success();
    }

    @GetMapping("/page")
    @Operation(summary = "开票历史")
    public CommonResult<PageResult<AppFinanceInvoiceVO>> page(AppFinanceInvoicePageReqVO reqVO) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        reqVO.setCustId(user.getId());
        reqVO.setCustType(FinanceCustomerTypeEnum.INDIVIDUAL.name());
        PageResult<FinanceInvoiceRecordDO> page = financeInvoiceRecordService.getPage(reqVO);
        return CommonResult.success(InvoiceConvert.INSTANCE.convertToPage3(page));
    }

    @GetMapping("/get")
    @Operation(summary = "开票详情")
    public CommonResult<AppFinanceInvoiceVO> detail(@RequestParam("id") Long id) {
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        FinanceInvoiceRecordDO financeInvoiceDO = financeInvoiceRecordService.getById(id);
        if (financeInvoiceDO == null || !financeInvoiceDO.getCustId().equals(user.getId())
                || FinanceCustomerTypeEnum.INDIVIDUAL.name().equals(financeInvoiceDO.getCustType())) {
            throw new ServiceException(ErrorCodeConstants.FINANCE_INVOICE_NOT_EXISTS);
        }
        return CommonResult.success(InvoiceConvert.INSTANCE.convert2(financeInvoiceDO));
    }
}
